package com.seh.contentcenter.service.es;

import com.seh.contentcenter.dto.GoodsDTO;
import com.seh.contentcenter.dto.UserDTO;
import com.seh.contentcenter.entity.Goods;
import com.seh.contentcenter.es.ESGoods;
import com.seh.contentcenter.feignclient.UserCenterFeignClient;
import com.seh.contentcenter.mapper.GoodsMapper;
import com.seh.contentcenter.repository.ESGoodsRepository;
import com.seh.contentcenter.repository.GoodsRepository;
import com.seh.contentcenter.service.Impl.GoodsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class ESGoodsService {
    @Autowired
    private GoodsRepository goodsRepository;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ESGoodsRepository esGoodsRepository;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private UserCenterFeignClient userCenterFeignClient;

    @Autowired
    private GoodsServiceImpl goodsService;

    /**
     * 添加新商品
     * @param goodsDTO
     */
    public Goods addGoods(GoodsDTO goodsDTO) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsDTO, goods);
        final Goods saveESGoods = transactionTemplate.execute((status) ->
            goodsRepository.save(goods));
        final ESGoods esGoods = new ESGoods();
        assert saveESGoods != null;
        BeanUtils.copyProperties(saveESGoods, esGoods);
        esGoods.setId(saveESGoods.getGoodsId() + "");
        try {
            esGoodsRepository.save(esGoods);
//            UserDTO userDTO = userCenterFeignClient.findUserInfoById(goodsDTO.getUserId());
//            userDTO.setUserId(goodsDTO.getUserId());
//            userDTO.setAddress(goodsDTO.getAddressName());
//            userDTO.setSendAddress(goodsDTO.getSendAddress());
//            userCenterFeignClient.updateUserInfo(userDTO);

            return saveESGoods;
        } catch (Exception e) {
            log.error(String.format("保存ES错误！%s", e.getMessage()));
            return null;
        }
    }

    public List<ESGoods> search(String keyword) {
        return esGoodsRepository.findByGoodsName(keyword);
    }

    /**
     * 通过elasticsearch查询所有商品
     * @param keyword
     * @return
     */
    public SearchHits<ESGoods> searchGoods(String keyword) {

        return esGoodsRepository.find(keyword);
    }

    /**
     * 通过id删除elasticsearch库中的商品信息
     * @param id
     */
    public void deleteGoods(String id) {
        esGoodsRepository.deleteById(id);
    }

    /**
     * 修改es库里商品的状态
     * @param goodsId
     * @return
     */
    public ResponseEntity<String> updateGoodsStatus(Integer goodsId, Integer goodsStatus) {
        Goods goods = goodsMapper.selectById(goodsId);
        if (goods != null) {
            // 修改属性
            goods.setGoodsStatus(goodsStatus);

            // 保存修改后的数据
            goodsMapper.updateById(goods);

            // 将修改后的数据同步到Elasticsearch
            ESGoods esGoods = new ESGoods();
            BeanUtils.copyProperties(goods, esGoods);
            esGoods.setId(goods.getGoodsId() + "");
            try {
                esGoodsRepository.save(esGoods);
            } catch (Exception e) {
                log.error(String.format("保存ES错误！%s", e.getMessage()));
            }

            return ResponseEntity.ok("Goods updated successfully");
        } else {
            // 处理数据不存在的情况
            return ResponseEntity.notFound().build();
        }

    }
}
